/*
 * Connect to MongoDB
 * @param {Function} success - Callback function to execute on successful connection
 * @param {Function} err - Callback function to execute on connection error
 */
module.exports = function (success, error) {
    const mongoose = require('mongoose');
    const { DB_HOST, DB_PORT, DB_NAME } = require('../config/config');

    mongoose.connect(`mongodb://${DB_HOST}:${DB_PORT}/${DB_NAME}`).then(() => {
        console.log('MongoDB connected successfully');
        success()
    }).catch(err => {
        console.error('MongoDB connection error:', err);
        // 检查 error 回调是否存在
        if (typeof error === 'function') {
            error(err); // 传递错误对象给回调
        }
    });

}